//
//  ReadingViewController.m
//  村医
//
//  Created by 融医网 on 2017/6/7.
//  Copyright © 2017年 融医联盟. All rights reserved.
//

#import "YCReadingViewController.h"
#import "YCReadingListViewController.h"
#import "YCReadingView.h"
#import "YCReadingPackageTableViewCell.h"
#import "Masonry.h"
#import "PayOrderViewController.h"
#import "YCReadingModel.h"


@interface YCReadingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property(nonatomic,strong)YCReadingView *readingView;
@property (nonatomic, assign)NSInteger package;
@property (nonatomic, copy)NSString *siteName;
@property (nonatomic, strong)NSArray *packages;
@end

@implementation YCReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.package = 0;
    self.packages = @[@"150元/月",@"400元/三个月"];
    
    self.navigationItem.title = @"心电阅片";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"列表" style:UIBarButtonItemStylePlain target:self action:@selector(list)];
    self.siteName = @"";
    [self createUI];
    [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLineLayout:) name:@"kChangeLineLayout" object:nil];
}
- (void)changeLineLayout:(NSNotification *)noti
{
    NSInteger isHeightChange = [noti.object integerValue];
    
    [self.readingView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(isHeightChange == YES ? 70 : 45);
    }];
}

-(void)createUI{
    
    
    YCReadingView *readingView = [[YCReadingView alloc]init];
    self.readingView = readingView;
    __weak typeof(self) weakSelf = self;
    readingView.HandleTextViewText = ^(NSString *tip) {
        [self presentMessageTips:tip];
    };
    readingView.endEditHandle = ^(NSString *siteName){
        weakSelf.siteName = siteName;
    };
    [self.view addSubview:readingView];
    
    UILabel *headerView = [[UILabel alloc]init];
    headerView.text = @"心电阅片包月套餐";
    [self.view addSubview:headerView];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 260) style:UITableViewStylePlain];
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    

    [self.tableView registerNib:[UINib nibWithNibName:@"YCReadingPackageTableViewCell" bundle:nil] forCellReuseIdentifier:@"YCReadingPackageTableViewCell"];
    

    [readingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(45);
    }];
    
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(readingView.mas_bottom).offset(0);
        make.height.mas_equalTo(44);
    }];
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(headerView.mas_bottom).offset(0);
        make.height.mas_equalTo(260);
    }];
    
    //提交按钮
    UIButton *submitBtn = [[UIButton alloc]init];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn setBackgroundColor:HexColor(0x3a79ea)];
    submitBtn.layer.masksToBounds = YES;
    submitBtn.layer.cornerRadius = 3;
    [submitBtn addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitBtn];
    
    
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(-69);
        make.height.mas_equalTo(44);
    }];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - tableViewDelegate,tableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifer = @"YCReadingPackageTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
       if (indexPath.section == 0) {
           YCReadingPackageTableViewCell *packageCell = (YCReadingPackageTableViewCell *)cell;
           
           if (indexPath.row == self.package) {
               cell.selected = YES;
           }else{
               cell.selected = NO;
               
           }
           packageCell.content = self.packages[indexPath.row];
           
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        [self.view endEditing:YES];
        self.package = indexPath.row;
        [self.tableView reloadData];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
     [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
     [self.view endEditing:YES];
}

#pragma mark - 提交
-(void)submitClick:(UIButton*)btn{
    
    btn.uxy_acceptEventInterval = 6.0f;
    [self.view endEditing:YES];
    if(!self.siteName || [self.siteName isEqualToString:@""]){
        [UIAlertController alertTitleTip:@"请输入详细站点名称"];
        return;
    }else if (self.package == 9999 ){
         [UIAlertController alertTitleTip:@"请选择阅片套餐"];
        return;
    }
    
    
    
    NSString *url = readingOrderSubmit;
    //[YCUserTool sharedTools].linkMobile
    NSDictionary *params = @{
                             @"hos_name":self.siteName,
                             @"link_mobile":[YCUserTool sharedTools].linkMobile,
                             @"yw_id":yw_id,
                             @"buy_type":self.package == 0 ? @"0" : @"1",
                             @"pay_money":self.package == 0 ? @"150" : @"400",
                             };
    [self presentLoadingTips:nil];
    
    [DownloadData post:url params:params success:^(id json) {
        [self dismissTips];
        if (json[@"order_id"]) {
            
            PayOrderViewController * payVC = [[PayOrderViewController alloc]init];
            
            YCReadingModel *model = [[YCReadingModel alloc]init];
            
            model.orderId = json[@"order_id"];
            model.payMoney = (self.package == 0 ? @"150" : @"400") ;
            model.hosName = self.siteName;
            
            payVC.readingModel = model;
//            payVC.orderId =  json[@"order_id"]  ;
//            payVC.orderMoney = [(self.package == 0 ? @"150" : @"400") floatValue];
//            payVC.orderName = (self.package == 0 ? @"150/月" : @"400/三个月");
            
            
            [self.navigationController pushViewController:payVC animated:YES];
        }
        
    } failure:^(NSError *error) {
        [self dismissTips];
    }];
}

#pragma mark - 列表
-(void)list{
    YCReadingListViewController *ReadingListVC = [[YCReadingListViewController alloc]init];
    [self.navigationController pushViewController:ReadingListVC animated:YES];
    
}
@end
